#include <Kharlia/Engine/Common.h>
#include <Kharlia/Engine/Init.h>

#include <Kharlia/Engine/Debug.h>

namespace kharlia { namespace engine {

namespace {

object GInnerModule;
object GModule;
shared_ptr<core::FLogger> GLogger;

}

object GetInnerModuleAttr(str name) {
    assert(!GInnerModule.is_none());
    return GInnerModule.attr(name);
}

object GetModuleAttr(str name) {
    assert(!GModule.is_none());
    return GModule.attr(name);
}

void SetModuleAttr(str name, object value) {
    assert(!GModule.is_none());
    GModule.attr(name) = value;
}

shared_ptr<core::FLogger> GetLogger() {
    return GLogger;
}

core::FLogger* GetLoggerPtr() {
    return GLogger.get();
}

void _ExitModule() {
    _ExitActorClasses();
    _ExitLevelClasses();
    _ExitWorldClasses();
    _ExitClientClasses();
    GInnerModule = object();
    GModule = object();
    GLogger.reset();
}

void _InitModule() {
    import("kharlia.core");
    object __engine = import("kharlia.__engine");
    object atexit = import("atexit");

    GLogger = core::FLogger::GetLogger("engine");
    GInnerModule = __engine;
    GModule = scope();

    atexit.attr("register")(make_function(&_ExitModule));

    _InitExceptions();
    _InitConverters();
    _InitUnits();

    _InitTileDataClasses();
    _InitActorClasses();
    _InitLevelClasses();
    _InitPlayerClasses();
    _InitWorldClasses();
    _InitResourceClasses();
    _InitEngineClasses();
    _InitClientClasses();
}

}} // namespace Kharlia

BOOST_PYTHON_MODULE(_engine) {
    kharlia::engine::_InitModule();
}